//
//  MineView.swift
//  shop
//
//  Created by chen on 2025/5/12.
//

import SwiftUI

struct MineView: View {
    
    @EnvironmentObject private var router: Router
    @EnvironmentObject private var authManager: AuthManager
    
    @Binding var selectTab: TabEnum
    
    @State private var logoutLoading: Bool = false
    
    var body: some View {
        ZStack{
            Color.gray.opacity(0.1)
                .ignoresSafeArea()
            
            VStack{
                if authManager.isLogin {
                    HStack(spacing: 10) {
                        Image("logo")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(width: 35, height: 35)
                        
                        HStack(spacing: 0) {
                            Text("帐号：")
                                .font(.system(size: 14))
                                .foregroundStyle(.gray)
                            
                            Text("\(authManager.phone.maskPhoneNumber())")
                                .font(.system(size: 16))
                        }
                        
                        Spacer()
                    }
                    .foregroundStyle(.black)
                    .padding(.horizontal, 16)
                    .padding(.top, 20)
                    .padding(.bottom, 10)
                } else {
                    Button {
                        authManager.toLogin()
                    } label: {
                        HStack(spacing: 10) {
                            Image(systemName: "person.circle")
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .frame(width: 35, height: 35)
                            
                            Text("点击登录")
                                .font(.system(size: 16))
                            
                            Spacer()
                        }
                        .foregroundStyle(.gray)
                        .padding(.horizontal, 16)
                    }
                    .padding(.top, 20)
                    .padding(.bottom, 10)
                }
                
                VStack(spacing: 15) {
                    HStack(spacing: 10) {
                        Text("余额")
                        Text("\(authManager.isLogin ? "0.00" : "*****")")
                        
                        Spacer()
                        
                        Button {
                            
                        } label: {
                            HStack(spacing: 0) {
                                Text("立即提现")
                            }
                            .padding(.vertical, 5)
                            .padding(.horizontal, 8)
                            .background(.black)
                            .foregroundStyle(.white)
                            .cornerRadius(5)
                            .overlay(
                                RoundedRectangle(cornerRadius: 5)
                                    .stroke(Color.white, lineWidth: 1)
                            )
                        }
                    }
                    
                    HStack(spacing: 0){
                        VStack(spacing: 5) {
                            Text("¥ \(authManager.isLogin ? "0.00" : "****")")
                            Text("今日预估")
                        }
                        .frame(maxWidth: .infinity)
                        
                        Text("|")
                        
                        VStack(spacing: 5) {
                            Text("¥ \(authManager.isLogin ? "0.00" : "****")")
                            Text("本月预估")
                        }
                        .frame(maxWidth: .infinity)
                    }
                }
                .padding(15)
                .background(Color.red)
                .cornerRadius(15)
                .foregroundStyle(.white)
                .font(.system(size: 15))
                .padding(.horizontal, 10)
                
                VStack(alignment: .leading, spacing: 15) {
                    Button(action:{
                        selectTab = .home
                    }) {
                        HStack(spacing: 10){
                            Image(systemName: "waveform.circle")
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .frame(width: 18, height: 18)
                            
                            Text("每日推荐")
                                .font(.system(size: 16))
                                .foregroundStyle(Color.black)
                            
                            Spacer()
                            
                            Image(systemName: "chevron.right")
                                .font(.system(size: 14))
                                .foregroundStyle(Color.black)
                        }
                    }
                    Divider()
                    
                    Button(action: {
                        selectTab = .rank
                    }) {
                        HStack(spacing: 10){
                            Image(systemName: "list.bullet.circle")
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .frame(width: 18, height: 18)
                            
                            Text("热门榜单")
                                .font(.system(size: 16))
                                .foregroundStyle(Color.black)
                            
                            Spacer()
                            
                            Image(systemName: "chevron.right")
                                .font(.system(size: 14))
                                .foregroundStyle(Color.black)
                        }
                    }
                    Divider()
                    
                    Button(action: {
                        
                    }) {
                        VStack(spacing: 0) {
                            HStack(spacing: 10){
                                Image(systemName: "link")
                                    .resizable()
                                    .aspectRatio(contentMode: .fit)
                                    .frame(width: 18, height: 18)
                                
                                Text("关于我们")
                                    .font(.system(size: 16))
                                    .foregroundStyle(Color.black)
                                
                                Spacer()
                                
                                Image(systemName: "chevron.right")
                                    .font(.system(size: 14))
                                    .foregroundStyle(Color.black)
                            }
                        }
                    }
                    
                }
                .padding(.horizontal, 10)
                .padding(.vertical, 15)
                .background(Color.white)
                .cornerRadius(10)
                .padding(.horizontal, 10)
                .padding(.top, 10)
                
                Spacer()
                
                if authManager.isLogin {
                    Button {
                        logout()
                    } label: {
                        HStack(spacing: 8) {
                            Text("退出登录")
                                .font(.system(size: 15))
                                .foregroundStyle(Color.white)
                            
                            if logoutLoading {
                                ProgressView()
                                    .progressViewStyle(CircularProgressViewStyle(tint: .white))
                            }
                        }
                        .padding(.vertical, 10)
                        .padding(.horizontal, 8)
                        .frame(maxWidth: .infinity)
                        .background(.red)
                        .cornerRadius(5)
                        .overlay(
                            RoundedRectangle(cornerRadius: 5)
                                .stroke(Color.red, lineWidth: 1)
                        )
                        .padding(.horizontal, 50)
                    }
                    
                    Spacer()
                }
            }
        }
        .navigationBarBackButtonHidden(true)
    }
    
    private func logout() {
        Task{
            logoutLoading = true
            await NetManager.server.httpGet(.logout)
            authManager.logout()
            logoutLoading = false
        }
    }
}

#Preview {
    MineView(selectTab: .constant(.home))
        .environmentObject(Router.shared)
        .environmentObject(AuthManager.shared)
}
